

#include "../log/xglog.h"
#include <unistd.h>
// 时间单位
enum class Time_Gap
{
    GAP_SECOND,
    GAP_MINUTE,
    GAP_HOUR,
    GAP_DAY,
};
// 扩展，支持以时间段为单位的滚动数组
class RollByTimeSink : public xglog::LogSink
{
public:
    RollByTimeSink(const std::string &basename, Time_Gap gap_type)
        : _basename(basename)
    {
        switch (gap_type)
        {
        case Time_Gap::GAP_SECOND:
            _gap_size = 1;
            break;
        case Time_Gap::GAP_MINUTE:
            _gap_size = 60;
            break;
        case Time_Gap::GAP_HOUR:
            _gap_size = 3600;
            break;
        case Time_Gap::GAP_DAY:
            _gap_size = 3600 * 24;
            break;
        }
        // 获取当前时间段
        _gap_count = _gap_size == 1 ? (xglog::util::Date::datetime()) : (xglog::util::Date::datetime()) % _gap_size;
        std::string pathname = createNewFileName();
        // 1,首先为该文件创建目录
        xglog::util::File::createDrector(xglog::util::File::path(pathname));
        // 2,打开文件,以二进制流的形式，并且写入是按照追加方式写入的
        _ofs.open(pathname, std::ios::binary | std::ios::app);
        assert(_ofs.is_open());
    }
    void log(const char *data, size_t len)
    {
        size_t cur_time = xglog::util::Date::datetime();
        if ((cur_time % _gap_size) != _gap_count)
        {
            _ofs.close();
            std::string pathname = createNewFileName();
            // 1,首先为该文件创建目录
            xglog::util::File::createDrector(xglog::util::File::path(pathname));
            // 2,打开文件,以二进制流的形式，并且写入是按照追加方式写入的
            _ofs.open(pathname, std::ios::binary | std::ios::app);
            assert(_ofs.is_open());
        }
        _ofs.write(data, len);
        assert(_ofs.good());
    }

private:
    std::string createNewFileName()
    {
        // 获取系统时间，以时间来作为系统名
        time_t t = xglog::util::Date::datetime();
        struct tm lt;
        localtime_r(&t, &lt);

        std::stringstream filename;
        filename << _basename;
        filename << lt.tm_year + 1900; // 年
        filename << lt.tm_mon + 1;     // 月
        filename << lt.tm_mday;        // 日
        filename << lt.tm_hour;        // 时
        filename << lt.tm_min;         // 分
        filename << lt.tm_sec;         // 秒
        filename << ".log";

        return filename.str();
    }

private:
    std::string _basename;
    size_t _gap_size;  // 时间间隔
    size_t _gap_count; // 当前是第几个时间段
    std::ofstream _ofs;
};


int main()
{
    xglog::logMessage logMsg(xglog::LogLevel::value::INFO, 10, "main.cc", "root", "创建套接字失败...");
    xglog::Formatter F;
    // xglog::Formatter F("%%%g11111222[%d{%H:%M:%S}][%t][%c][%f:%l][%p]%T%m%n");
    std::string str = F.format(logMsg); // 重复定义时出错(通过条件编译解决)
    std::cout << str << std::endl;
    xglog::SinkPtr stdout_sink = xglog::SinkFactory::createSink<xglog::StdoutSink>();
    xglog::SinkPtr file_sin = xglog::SinkFactory::createSink<xglog::FileSink>("./logfile/test.log");
    // 一个文件默认最多写入1M数据
    xglog::SinkPtr roll_sink = xglog::SinkFactory::createSink<xglog::RollBySizeSink>("./logfile/base-", 1024 * 1024);
    stdout_sink->log(str.c_str(), str.size());
    file_sin->log(str.c_str(), str.size());
    // 点歌 凤凰花开的路口 房东的猫
    xglog::SinkPtr roll_sink = xglog::SinkFactory::createSink<RollByTimeSink>("./logfile/base-", Time_Gap::GAP_SECOND);
    time_t old = xglog::util::Date::datetime();
    while (xglog::util::Date::datetime() < old + 5)
    {
        roll_sink->log(str.c_str(), str.size());
    }
    return 0;
}